/*
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2, or (at your option) any later version. This
 * program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307, USA.
 * http://www.gnu.org/copyleft/gpl.html
 */
package net.l1j.server.clientpackets;

import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

import net.l1j.Config;
import net.l1j.server.ClientThread;
import net.l1j.server.model.L1World;
import net.l1j.server.model.id.SystemMessageId;
import net.l1j.server.model.instance.L1DollInstance;
import net.l1j.server.model.instance.L1ItemInstance;
import net.l1j.server.model.instance.L1PcInstance;
import net.l1j.server.model.instance.L1PetInstance;
import net.l1j.server.serverpackets.S_ServerMessage;
import net.l1j.server.serverpackets.S_SystemMessage;
//add 防止複製道具 code (1/2) 
import net.l1j.extra.L1CheckPcItem; 
// add end
public class C_DropItem extends ClientBasePacket {
	private static final String C_DROP_ITEM = "[C] C_DropItem";

	private static final Logger _log = Logger.getLogger("item");

	public C_DropItem(byte[] decrypt, ClientThread client) throws Exception {
		super(decrypt);

		int x = readH();
		int y = readH();
		int objectId = readD();
		int count = readD();
//		防洗物品
		count = Math.abs(count);
		count = Math.min(count,2000000000);
		//防洗物品 end

		L1PcInstance pc = client.getActiveChar();
		if (pc.isGhost()) {
			return;
		}

		L1ItemInstance item = pc.getInventory().getItem(objectId);
		if (item != null) {
			if (!item.getItem().isTradable()) {
				pc.sendPackets(new S_ServerMessage(SystemMessageId.$210, item.getItem().getName()));
				return;
			}

			Object[] petlist = pc.getPetList().values().toArray();
			for (Object petObject : petlist) {
				if (petObject instanceof L1PetInstance) {
					L1PetInstance pet = (L1PetInstance) petObject;
					if (item.getId() == pet.getItemObjId()) {
						pc.sendPackets(new S_ServerMessage(SystemMessageId.$210, item.getItem().getName()));
						return;
					}
				}
			}
			//		      物品丟地上系統會自動刪除 by bill00148 
			if (pc.isGm() || item.getItemId() == 40010 || // 治癒藥水
					item.getItemId() == 40011 ||          // 強力治癒藥水
					item.getItemId() == 40012 ||          // 終極治癒藥水
					item.getItemId() == 40013 ||          // 自我加速藥水
					item.getItemId() == 40056 ||          // 肉
					item.getItemId() == 40057 ||          // 漂浮之眼肉
					item.getItemId() == 40060 ||          // 胡蘿蔔
					item.getItemId() == 40070 ||          // 進化果實
					item.getItemId() == 40725 ||          // 南瓜糖果
					item.getItemId() == 40726 ||          // 南瓜種子
					item.getItemId() == 88 ||             // 潘的角
					item.getItemId() == 40045 ||          // 紅寶石
					item.getItemId() == 40494 ||          // 純粹的米索莉塊
					item.getItemId() == 40495 ||          // 米索莉線
					item.getItemId() == 40520 ||          // 精靈粉末
					item.getItemId() == 40521 ||          // 精靈羽翼
					item.getItemId() == 40508 ||          // 奧裡哈魯根
					item.getItemId() == 40493 ||          // 魔法笛子
					item.getItemId() == 40032 ||          // 伊娃的祝福
					item.getItemId() == 41310) {          // 勝利果實
		                }
			else if (pc.getLevel() <= 99){
            pc.getInventory().removeItem(objectId,count); 
            pc.sendPackets(new S_SystemMessage("物品請勿丟地上，已遭系統刪除。"));
            return;
            }
//	      物品丟地上系統會自動刪除 by bill00148

//			 add 防止複製道具 code (2/2) 
			L1CheckPcItem checkPcItem = new L1CheckPcItem(); 
			boolean isCheat = checkPcItem.checkPcItem(item, pc); 
			if (isCheat) { 
			L1World.getInstance().broadcastServerMessage("\\fZ【" + pc.getName() + "】丟棄違法道具！(刪除違法道具)"); 
			return; 
			} 
//			 add end
			Object[] dollList = pc.getDollList().values().toArray();
			for (Object dollObject : dollList) {
				if (dollObject instanceof L1DollInstance) {
					L1DollInstance doll = (L1DollInstance) dollObject;
					if (item.getId() == doll.getItemObjId()) {
						pc.sendPackets(new S_ServerMessage(SystemMessageId.$1181));
						return;
					}
				}
			}
			if (item.isEquipped()) {
				pc.sendPackets(new S_ServerMessage(SystemMessageId.$125));
				return;
			}
			if (item.getBless() >= 128) { // 封印的裝備
				pc.sendPackets(new S_ServerMessage(SystemMessageId.$210, item.getItem().getName()));
				return;
			}
			pc.getInventory().tradeItem(item, count, L1World.getInstance().getInventory(x, y, pc.getMapId()));
			pc.turnOnOffLight();

			if (Config.LOGGING_ITEM_DROP) {
				LogRecord record = new LogRecord(Level.INFO, "<掉落>");
				record.setLoggerName("item");
				record.setParameters(new Object[] { pc, item });
				_log.log(record);
			}
		}
	}

	@Override
	public String getType() {
		return C_DROP_ITEM;
	}
}
